package com.hhit.collaborative_learning.management;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.dao.BBS_TopicDao;
import com.hhit.collaborative_learning.dao.BlogDao;
import com.hhit.collaborative_learning.dao.impl.BBS_TopicDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BlogDaoImpl;
import com.hhit.collaborative_learning.dao.impl.PublicDaoImplMySql;
import com.hhit.collaborative_learning.entity.SearchResult;
import com.hhit.collaborative_learning.entity.SearchResultPage;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.DateUtil;

/**
 * Servlet implementation class SearchServlet
 */
@WebServlet("/SearchServlet")
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static int pagesiz=15;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		String tyle = request.getParameter("tyle");
		String searchc = request.getParameter("searchc").trim();
		String pageNow = request.getParameter("pagenow");
		if(request.getMethod().equals("GET")){
			searchc=new String(searchc.getBytes("iso8859-1"),"utf-8");
		}
		int pagenow=Integer.valueOf(pageNow);
		Connection con=DBAccess.getInstance().getConn();
		PublicDaoImplMySql dao=new PublicDaoImplMySql();
		SearchResultPage srP=new SearchResultPage();
		switch (tyle) {
		case "1":
//			System.out.println("博客");
			srP = searchBlog(con,searchc,pagenow);
			break;
		case "2":
//			System.out.println("帖子");
			srP = searchBBSTopic(con,searchc,pagenow);
			break;
		case "3":
//			System.out.println("资源");
			srP = dao.queryData(searchc, pageNow);
			break;
		case "4":
//			System.out.println("提问");
			srP = dao.queryQuestion(searchc, pageNow);
			break;
		default:
//			System.out.println("未知类型：  " + tyle);
			break;
		}
		DBAccess.getInstance().close(con, null, null);
		request.setAttribute("resultPage", srP);
		request.setAttribute("type", tyle);
		request.setAttribute("searchc", searchc);
		request.getRequestDispatcher("main_search.jsp").forward(request, response);
	}

	// 博客
	private SearchResultPage searchBlog(Connection con,String searchcontent,int pagenow) {
		SearchResultPage srP=new SearchResultPage();
		List<SearchResult> list = new ArrayList<SearchResult>();
		BlogDao bd=new BlogDaoImpl();
		ResultSet rsBlog=bd.queryforSearch(con, (pagenow - 1) * pagesiz,pagesiz, searchcontent, "desc", "b.readNum");
		list=DateUtil.formatBlogRs(rsBlog);
		int count=bd.getcount(con, "b.userId=u.userId and (b.blogName like '%" + searchcontent
					+ "%' or u.userNickName like '%" + searchcontent + "%' or b.tag like '%"+searchcontent+"%' or b.blogDesc like '%"+searchcontent+"%' or b.category like '%"+searchcontent+"%' or b.category_CN like '%"+searchcontent+"%')");
		int totalPage = (int) Math.ceil((double)count / (double)pagesiz);
		srP.list=list;
		srP.count=count;
		srP.currentPage=pagenow;
		srP.totalPage=totalPage;
		return srP;
	}

	// 帖子
	private SearchResultPage searchBBSTopic(Connection con,String searchcontent,int pagenow) {
		SearchResultPage srP=new SearchResultPage();
		List<SearchResult> list = new ArrayList<SearchResult>();
		BBS_TopicDao tdao = new BBS_TopicDaoImpl();
		ResultSet rsTopic = tdao.queryforSearch(con, (pagenow - 1) * pagesiz,pagesiz, searchcontent, "desc", "bt.Hit");
		list=DateUtil.formatTopicRe(rsTopic);
		int count= tdao.getcount(con, "bt.Title like '%"+searchcontent+"%' or u.userNickName like '%"+searchcontent+"%'");
		int totalPage = (int) Math.ceil((double)count / (double)pagesiz);
		srP.list=list;
		srP.count=count;
		srP.currentPage=pagenow;
		srP.totalPage=totalPage;
		return srP;
	}
	// 资源
	private SearchResultPage searchdownload(Connection con,String searchcontent,int pagenow) {
		SearchResultPage srP=new SearchResultPage();
		List<SearchResult> list = new ArrayList<SearchResult>();
		
		return srP;
	}
	// 提问
	private SearchResultPage searchQuestion(Connection con,String searchcontent,int pagenow) {
		SearchResultPage srP=new SearchResultPage();
		List<SearchResult> list = new ArrayList<SearchResult>();
		
		return srP;
	}
}
